package ace.module.account.core.impl.manager.provider;

import ace.cmp.spring.util.core.LogUtils;
import ace.module.common.api.model.input.AppIdListInput;
import ace.module.upms.api.UpmsOrganizationApi;
import ace.module.upms.api.model.input.UpmsOrgBindAccountInput;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * .
 * @author Caspar
 *
 */
@Slf4j
@AllArgsConstructor
@Component
public class UpmsOrganizationProvider {

  private static final String SERVICE_EXCEPTION_MESSAGE = "组织服务调用失败";
  private final UpmsOrganizationApi upmsOrganizationApi;

  public void saveOrgAccountRelation(Long upmsAppId, Long iamAccountId, List<Long> orgIds) {

    List<UpmsOrgBindAccountInput> upmsRoleBindAccountInputs = orgIds.stream()
        .map(orgId -> UpmsOrgBindAccountInput.builder()
            .upmsOrgId(orgId)
            .iamAccountId(iamAccountId)
            .build())
        .collect(Collectors.toList());

    AppIdListInput<UpmsOrgBindAccountInput> input = AppIdListInput.<UpmsOrgBindAccountInput>builder()
        .appId(upmsAppId)
        .data(upmsRoleBindAccountInputs)
        .build();

    LogUtils.logThrowExceptionOrDirectThrowBusinessException(SERVICE_EXCEPTION_MESSAGE, () -> this.upmsOrganizationApi.saveOrgAccountRelation(input).check());

  }
}
